package com.epfresh.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.R;
import com.epfresh.bean.MsgTag;

import java.util.List;

public class MsgTagAdapter extends RecyclerView.Adapter<MsgTagAdapter.ViewHolder> implements View.OnClickListener {
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position, Object data);
    }
    private List<MsgTag> msgTagList;

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    OnMsgTagSelectListener onMsgTagSelectListener;

    public OnMsgTagSelectListener getOnMsgTagSelectListener() {
        return onMsgTagSelectListener;
    }

    public void setOnMsgTagSelectListener(OnMsgTagSelectListener onMsgTagSelectListener) {
        this.onMsgTagSelectListener = onMsgTagSelectListener;
    }

    public MsgTagAdapter(List<MsgTag> category) {
        this.msgTagList = category;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public void setMsgTagList(List<MsgTag> posts) {
        this.msgTagList = posts;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_msg_tag, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MsgTag post = msgTagList.get(position);

        holder.title.setText(post.getName());
        holder.itemView.setTag(R.id.item_key_position, position);
        holder.title.setTag(R.id.item_key_position, position);
        holder.title.setOnClickListener(this);
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) holder.title.getLayoutParams();

        if (position == 0) {
            lp.leftMargin = LocalDisplay.dp2px(12);
        } else {
            lp.leftMargin = 0;
        }

        /*if (post.isSelect()) {
            holder.title.setTextColor(Color.parseColor("#009983"));
            holder.title.setBackgroundResource(R.drawable.dialog_msg_tag_select_bg);
        } else {
            holder.title.setTextColor(Color.parseColor("#959595"));
            holder.title.setBackgroundResource(R.drawable.dialog_msg_tag_normal_bg);
        }*/
    }


    @Override
    public int getItemCount() {
        return msgTagList != null ? msgTagList.size() : 0;
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;

            if (v.getId() == R.id.tv_msg) {
                if (position < msgTagList.size()) {
                    MsgTag msgTag = msgTagList.get(position);
                    if (msgTag.isSelect()) {
                        msgTag.setSelect(false);
                    } else {
                        msgTag.setSelect(true);
                    }
                    notifyItemChanged(position);
                    if (onMsgTagSelectListener != null){
                        onMsgTagSelectListener.onMsgTagSelect(msgTag.isSelect(),msgTag);
                    }
                }
            } else if (onRecyclerViewItemClickListener != null && position < msgTagList.size()) {
                onRecyclerViewItemClickListener.onItemClick(v, position, msgTagList.get(position));
            }
        }
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView title;


        ViewHolder(final View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tv_msg);
        }
    }

    public interface OnMsgTagSelectListener {
        void onMsgTagSelect(boolean isSelect, MsgTag msgTag);
    }

}
